package com.yike.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.product.entity.GoodsUnit;
import com.yike.product.mapper.GoodsUnitMapper;
import com.yike.product.param.GoodsUnitParam;
import com.yike.product.service.GoodsUnitService;
import com.yike.product.param.GoodsUnitPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.product.vo.GoodsUnitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品单位 服务实现类
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@Service
public class GoodsUnitServiceImpl extends BaseServiceImpl<GoodsUnitMapper, GoodsUnit> implements GoodsUnitService {

    @Autowired
    private GoodsUnitMapper goodsUnitMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveGoodsUnit(GoodsUnitParam goodsUnitParam) throws Exception {
        // 先判断名称是否重复
        GoodsUnit goodsUnit = goodsUnitMapper.selectOne(Wrappers.lambdaQuery(GoodsUnit.class)
                .eq(GoodsUnit::getName, goodsUnitParam.getName()));
        if (goodsUnit != null) {
            throw new BusinessException("当前单位已存在，请勿重复添加");
        }
        goodsUnit = new GoodsUnit();
        BeanUtils.copyProperties(goodsUnitParam, goodsUnit);
        goodsUnit.setCreateBy(LoginUtil.getUserId());
        return super.save(goodsUnit);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsUnit(GoodsUnitParam goodsUnitParam) throws Exception {
        // 先获取信息
        GoodsUnit goodsUnit = goodsUnitMapper.selectById(goodsUnitParam.getId());
        if (goodsUnit == null) {
            throw new BusinessException("单位信息异常");
        }
        // 防止修改的时候和已有名称相同
        Integer checkNum = goodsUnitMapper.selectCount(Wrappers.lambdaQuery(GoodsUnit.class)
                .eq(GoodsUnit::getName, goodsUnitParam.getName())
                .ne(GoodsUnit::getId, goodsUnitParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前单位已存在，请勿重复添加");
        }
        BeanUtils.copyProperties(goodsUnitParam, goodsUnit);
        goodsUnit.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(goodsUnit);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodsUnit(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public GoodsUnitVo getGoodsUnit(Long id) throws Exception {
        return goodsUnitMapper.getGoodsUnitVo(id);
    }

    @Override
    public Paging<GoodsUnitVo> getGoodsUnitPageList(GoodsUnitPageParam goodsUnitPageParam) throws Exception {
        Page<GoodsUnit> page = new PageInfo<>(goodsUnitPageParam);
        IPage<GoodsUnitVo> iPage = goodsUnitMapper.getGoodsUnitList(page, goodsUnitPageParam);
        return new Paging<GoodsUnitVo>(iPage);
    }

}
